利用循环队列实现舞伴配对

#ifndef _CYCLELINE_H_
#define _CYCLELINE_H_
#endif     
#include<stdlib.h>
#include <string.h>           
#include "stdio.h"
#include "math.h"
#define MaxSize  100

typedef int status;

typedef char DataType;


typedef struct
{
    
    DataType name[20];
    
 }Line1;
 
typedef struct
{
    Line1 LinSize[MaxSize];
    
    int rear;
    
    int count;
    
    int top;
    
 }Line;
 
 
 
 void InitLin (Line *s)
 
 {
     s->rear=s->top=s->count=0;
     
 }
 
    
   status InsertWLin (Line *s)
   {
       
       char w[20];
    
    int n1;
    
    int i;
       
       if (s-
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环队列可以用来实现舞伴配对问题。我们可以将男女分别存储在两个循环队列中,每次从男队和女队的队头各出一个人配成舞伴,如果两队初始人数不等,则较长的那一队中未配对者等待下一轮舞曲。具体实现步骤如下: 1. 初始化两个循环队列,分别存储男女。 2. 输入舞会轮数,每轮循环队列中出队两个人,分别为男女舞伴。 3. 判断两个队列是否为空,如果为空则输出未配对者姓名。 4. 输出每一轮舞伴配对名单。 代码实现如下: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 100 typedef struct { char name[20]; int gender; // 0表示女性,1表示男性 } Person; typedef struct { Person data[MAXSIZE]; int front, rear; } Queue; void InitQueue(Queue *Q) { Q->front = Q->rear = 0; } int QueueEmpty(Queue Q) { return Q.front == Q.rear; } int QueueFull(Queue Q) { return (Q.rear + 1) % MAXSIZE == Q.front; } void EnQueue(Queue *Q, Person x) { if (QueueFull(*Q)) { printf("Queue is full!\n"); exit(1); } Q->data[Q->rear] = x; Q->rear = (Q->rear + 1) % MAXSIZE;} Person DeQueue(Queue *Q) { if (QueueEmpty(*Q)) { printf("Queue is empty!\n"); exit(1); } Person x = Q->data[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; return x; } void Match(Queue *male, Queue *female, int rounds) { int i; for (i = 1; i <= rounds; i++) { printf("Round %d:\n", i); if (QueueEmpty(*male) || QueueEmpty(*female)) { printf("No enough people!\n"); break; } Person m = DeQueue(male); Person f = DeQueue(female); printf("%s and %s are matched!\n", m.name, f.name); if (male->rear > male->front) { EnQueue(male, m); } if (female->rear > female->front) { EnQueue(female, f); } } if (!QueueEmpty(*male)) { printf("The following males are not matched:\n"); while (!QueueEmpty(*male)) { Person m = DeQueue(male); printf("%s\n", m.name); } } if (!QueueEmpty(*female)) { printf("The following females are not matched:\n"); while (!QueueEmpty(*female)) { Person f = DeQueue(female); printf("%s\n", f.name); } } } int main() { Queue male, female; InitQueue(&male); InitQueue(&female); // 初始化男女队列 Person m1 = {"Tom", 1}; Person m2 = {"Jack", 1}; Person m3 = {"Mike", 1}; EnQueue(&male, m1); EnQueue(&male, m2); EnQueue(&male, m3); Person f1 = {"Lucy", 0}; Person f2 = {"Lily", 0}; Person f3 = {"Mary", 0}; EnQueue(&female, f1); EnQueue(&female, f2); EnQueue(&female, f3); Match(&male, &female, 3); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值